﻿using System;
using System.IO;
using iTunesLib;

namespace iTunes.Scrubber.MediaItems
{
    public class TVShowItem : BaseClasses.MediaBase<TVShowItem>
    {
        public String Show { get; set; }

        public Int32? Season { get; set; }
        public Int32? Episode { get; set; }

        public String Description { get; set; }
        public String ShortDescription
        {
            get
            {
                this.Description = this.Description ?? String.Empty;
                return this.Description.Substring(0, Math.Min(255, this.Description.Length));
            }
        }

        public Boolean IsHD { get; set; }

        public override TVShowItem UpdateMetaData()
        {
            Console.WriteLine("Updating: {0}\r\n\t{1} {2:D2}x{3:D2} - {4}", this.Location, this.Show, this.Season, this.Episode, this.Title);
            
            if (Constants.NOCHANGES || !this.IsDirty)
                return this;

            try
            {
                MemoryStream stream = new MemoryStream();

                try
                {
                    // Get Handle To The Media File
                    mp4v2.MP4FileHandle ptr = libmp4v2.MP4Modify(this.Location, new mp4v2.uint32_t(0), new mp4v2.uint32_t(0));

                    if (ptr.Value != new IntPtr(0))
                    {
                        // Allocate Memory For Tags Struct
                        mp4v2.MP4Tags tags = libmp4v2.MP4TagsAlloc();

                        try
                        {
                            // Get Current Tags
                            libmp4v2.MP4TagsFetch(tags, ptr);

                            #region TV Related MetaData

                            // Set Show
                            if (!String.IsNullOrWhiteSpace(this.Show))
                                libmp4v2.MP4TagsSetTVShow(tags, this.Show);

                            // Set Season
                            libmp4v2.MP4TagsSetTVSeason(tags, this.Season);

                            // Set Episode
                            libmp4v2.MP4TagsSetTVEpisode(tags, this.Episode);

                            #endregion

                            // Set Title
                            if (!String.IsNullOrWhiteSpace(this.Title))
                                libmp4v2.MP4TagsSetName(tags, this.Title);

                            // Set ShortDescription
                            if (!String.IsNullOrWhiteSpace(this.ShortDescription))
                                libmp4v2.MP4TagsSetDescription(tags, this.ShortDescription);

                            // Set LongDescription
                            if (!String.IsNullOrWhiteSpace(this.Description))
                                libmp4v2.MP4TagsSetLongDescription(tags, this.Description);

                            // Set HD Flag
                            libmp4v2.MP4TagsSetHDVideo(tags, this.IsHD);

                            // Save The Tags
                            libmp4v2.MP4TagsStore(tags, ptr);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }

                        // DeAllocate Tag Struct
                        libmp4v2.MP4TagsFree(tags);
                    }

                    // Close The File
                    libmp4v2.MP4Close(ptr);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                // Refresh iTunes
                this.LocalTrack.UpdateInfoFromFile();

                // Update TrackNo
                if (this.Episode.HasValue)
                    this.LocalTrack.TrackNumber = this.Episode.Value;

                // Update TV Flag
                this.LocalTrack.VideoKind = ITVideoKind.ITVideoKindTVShow;

                this.IsDirty = false;
            }
            catch (Exception ex) { Console.WriteLine(ex.Message); }

            return this;
        }
    }
}
